package com.yuqian.JavaSe.leetcode.easy;

import com.yuqian.JavaSe.leetcode.util.TreeNode;

/**
 * Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

判断两个树  是否相等，  
 * @author yuqian
 *
 */
public class SameTree {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==q){
            return true;
        }else{
           if(p!=null&&q!=null){
               if(p.val==q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right)){
                   return true;
               }else{
                   return false;
               }
           }else{
               return false;
           }
        }
        //更简洁的写法
        //if(p == null && q == null) return true;
        //if(p == null || q == null) return false;
        //return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
